﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Lead2012.Models;
using Lead2012.ViewModels;
using Lead2012.Helpers;
using Lead2012.Business;

namespace Lead2012.Repository
{
    public class LeadStatusManagementRepository
    {
        // Create an instance of entities framework
        Lead2012Entities entities = new Lead2012Entities();

        public void GetModel(LeadStatusViewModel model, string keywords, string sortBy, string sortOrder, int pageIndex, int pageSize)
        {
            int counts = 0;
            model.ItemList = GetAllLeadStatus(keywords, sortBy, sortOrder, pageIndex, pageSize, ref counts);            

            // Calculate paging
            if (counts > 0)
            {
                model.TotalPage = (counts % pageSize == 0) ? counts / pageSize : (counts / pageSize + 1);
                model.TotalRecord = counts;
                model.PageIndex = pageIndex;
                model.PageSize = pageSize;
            }
        }

        public void GetModel(LeadStatusViewModel model, int id)
        {
            model.LeadStatusInfo = GetSingleBy(id);
            model.MilestoneList = GetAllMilestone();
        }

        #region #Common
        public List<LeadStatusItem> GetAllLeadStatus(string keywords, string sortBy, string sortOrder, int page, int take, ref int counts)
        {
            List<LeadStatusItem> list = new List<LeadStatusItem>();

            // Get milestone / actions
            var milestoneList = new MilestoneRepository().GetAll();
            var items = new LeadStatusRepository().GetAll();
            
            // Add to list
            foreach (var item in items)
            {
                LeadStatusItem model = new LeadStatusItem();
                model.StatusId = item.StatusId;
                model.Title = item.Title;
                model.Color = item.Color;
                model.OrderId = item.OrderId;
                model.MilestoneId = item.MilestoneId;
                model.MilestoneTitle = milestoneList.Where(c => c.Id == item.MilestoneId).FirstOrDefault().Title;
                list.Add(model);
            }

            counts = list.Count;

            return list.OrderBy(c => c.OrderId).ToList();
        }
        public int GetOrderId()
        {
            List<LeadStatusItem> list = new List<LeadStatusItem>();
            int OId = 0;
            // Get Status
            var items = new LeadStatusRepository().GetAll();
            OId = items.Max(obj => obj.OrderId)+1;
            return OId;

        }

        public void Update(LeadStatusItem _object)
        {
            entities.SaveChanges();
        }
        
        public List<ItemFilter> GetAllMilestone()
        {
            List<ItemFilter> list = new List<ItemFilter>();

            var items = (from c in entities.Milestones select c).ToList();

            foreach (var item in items)
            {
                ItemFilter model = new ItemFilter();
                model.ID = item.Id.ToString();
                model.ItemName = item.Title;
                list.Add(model);
            }

            return list;
        }

        public LeadStatusItem GetSingleBy(int id)
        {
            LeadStatusItem model = new LeadStatusItem();

            var item = (from c in entities.LeadStatuses where c.StatusId == id select c).FirstOrDefault();
            if (item != null)
            {
                model.StatusId = item.StatusId;
                model.Title = item.Title;
                model.Color = item.Color;
                model.OrderId = item.OrderId;
                model.MilestoneId = item.MilestoneId;
                
            }

            return model;
        }
        #endregion
    }
}